// 服务事项接口 issuesPort

// import request from '@/utils/request';

// 查询列表
export function getissuesList(query) {
  return request({
    url: '/ms/matter/sysServiceItemManagement/list.do',
    method: 'get',
    params: query,
  });
}

/**
 * 新建事项
 * @param {Object} data - 要新建的事项数据对象
 * @returns {Promise<AxiosResponse>} - 返回请求的Promise对象
 * @throws {Error} 当参数无效时抛出错误
 */
export function createIssues(data) {
  // 构造请求数据
  const requestData = { ...data };
  return request({
    url: '/ms/matter/sysServiceItemManagement/save.do',
    method: 'post',
    data: requestData,
    headers: {
      'Content-Type': 'application/json',
    },
  });
}

/**
 * 修改事项
 * @param {Object} data - 要新建的事项数据对象
 * @returns {Promise<AxiosResponse>} - 返回请求的Promise对象
 * @throws {Error} 当参数无效时抛出错误
 */
export function editIssues(data) {
  // 构造请求数据
  const requestData = { ...data };
  return request({
    url: '/ms/matter/sysServiceItemManagement/update.do',
    method: 'post',
    data: requestData,
    headers: {
      'Content-Type': 'application/json',
    },
  });
}

/**
 * 删除指定ID的事项
 * @param {Array<number|string>} ids - 要删除的事项ID数组
 * @returns {Promise<AxiosResponse>} - 返回请求的Promise对象
 * @throws {Error} 当参数无效时抛出错误
 */
export function deletIssues(ids) {
  // 参数验证
  if (!Array.isArray(ids)) {
    return Promise.reject(new TypeError('参数必须是数组'));
  }

  if (ids.length === 0) {
    return Promise.reject(new Error('ID列表不能为空'));
  }

  // 验证每个ID是否有效
  const invalidIds = ids.filter(
    (id) =>
      id === null ||
      id === undefined ||
      (typeof id !== 'number' && typeof id !== 'string') ||
      (typeof id === 'string' && id.trim() === '')
  );

  if (invalidIds.length > 0) {
    return Promise.reject(new Error(`包含无效的ID: ${invalidIds.join(', ')}`));
  }

  // 构造请求数据
  const requestData = ids.map((id) => ({ id }));

  return request({
    url: '/ms/matter/sysServiceItemManagement/delete.do',
    method: 'post',
    data: requestData,
    headers: {
      'Content-Type': 'application/json',
    },
  });
}

/**
 * 查看指定事项详情
 * @param {number|string} id - 要查看的事项ID
 * @returns {Promise<AxiosResponse>} - 返回请求的Promise对象
 * @throws {Error} 当参数无效时抛出错误
 */

export function getIssuesDetail(id) {
  // 参数验证
  if (
    id === null ||
    id === undefined ||
    (typeof id !== 'number' && typeof id !== 'string') ||
    (typeof id === 'string' && id.trim() === '')
  ) {
    return Promise.reject(new Error('ID无效'));
  }
  return request({
    url: `ms/matter/sysServiceItemManagement/get.do?id=${id}`,
    method: 'get',
  });
}
